<?php
/**
 * Class UserInterface_Controller_AbstractBase
 */
abstract class UserInterface_Controller_AbstractBase
{
    /**
     * @var UserInterface_InputProcessor
     */
    protected $inputProcessor;

    /**
     * @var UserInterface_IInputSecurity
     */
    protected $inputSecurity;

    /**
     * @var
     */
    protected $inputMapperFactory;

    /**
     * @param array $params
     */
    protected function renderAjaxResponseAndStop(array $params)
    {
        // get new csrf token
        $params['csrf_token'] = $this->inputSecurity->get_csrf_hash();
        header('Content-Type: application/json');
        die(json_encode($params));
    }

    /**
     * @param Exception $e
     */
    public function printException(Exception $e)
    {
        if ($this->inputProcessor->inPost('ajax')) {
            $this->renderAjaxResponseAndStop(array('error' => true, 'message' => $e->getMessage()));
        }

        if (!headers_sent()) {
            $this->renderHeader("ERROR!", "body { background-color:#000; }");
        }
        echo "<pre>";
        echo "Uncaught <b>" . get_class($e) . "</b>! " . $e->getMessage() . "\n Stack trace:\n";
        echo $e->getTraceAsString();
        echo "</pre>";
        echo "<div style='text-align:center;'>";
        echo "<img src='assets/image/guru_meditation.gif' alt=''>";
        echo "</div>";

        $this->renderFooter();
    }

    /**
     * @param Exception $e
     */
    public function handleException(Exception $e)
    {
        self::printException($e);
        die();
    }

    /**
     * @param string $title
     * @param string $extraCSS
     */
    public function renderHeader($title, $extraCSS = '')
    {
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
            <meta http-equiv="content-style-type" content="text/css">
            <meta name="<?php echo $this->inputSecurity->get_config('csrf_token_name'); ?>"
                  content="<?php echo $this->inputSecurity->get_csrf_hash(); ?>">
            <link rel="stylesheet" href="assets/css/main.css" type="text/css">
            <title><?php echo $title; ?></title>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
            <script src="assets/js/main.js" type="text/javascript"></script>
            <?php if ($extraCSS) : ?>
                <style type="text/css">
                    <?php echo $extraCSS; ?>
                </style>
            <?php endif; ?>
        </head>
        <body>
    <?php
    }

    /**
     *
     */
    protected function renderFooter()
    {
        ?></body></html><?php
    }

    /**
     * @param Infrastructure_Bootstrap $bootstrap
     */
    public function __construct(Infrastructure_Bootstrap $bootstrap)
    {
        set_exception_handler(array($this, "handleException"));

        $this->mapperFactory = $bootstrap->getMapperFactory();
        $this->inputProcessor = $bootstrap->getInputProcessor();
        $this->inputSecurity = $bootstrap->getInputSecurity();

    }
}
